package java_option.player.javacv.ext;

import java_option.player.javacv.core.FrameAudioGrabberThread;
import java_option.player.javacv.core.FrameFIFO;
import java_option.player.javacv.core.FrameVideoGrabberThread;
import java_option.player.javafx.MainApplication;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.WindowEvent;

/**
 * 视频进度
 * 
 * @author java红了 <br/>
 *         2020-9-2923:53:05
 */
public class VideoSliderUpdate {

	private static VideoSliderUpdate instance;

	private Slider mProgressSlider;
	private StackPane mStackPane;

	/**
	 * slider滑动状态 true表示当前正在手工滑动
	 */
	private boolean isModed;

	private VideoSliderUpdate() {

	}

	public static VideoSliderUpdate getInstance() {
		synchronized (VideoSliderUpdate.class) {
			if (instance == null) {
				instance = new VideoSliderUpdate();
			}
		}
		return instance;
	}

	public void init(Slider progressSlider) {
		this.mProgressSlider = progressSlider;

		this.mProgressSlider.setMin(0.01);
		this.mProgressSlider.setMax(100.0);
		MainApplication.primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent window) {
				mStackPane = (StackPane) mProgressSlider.lookup(".track");
				float defaultProgress = 0.01f;
				setSliderStyle(defaultProgress);

				mProgressSlider.valueProperty().addListener(new ChangeListener<Number>() {

					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
						float newProgress = newValue.floatValue() / 100;
						setSliderStyle(newProgress);
					}
				});

				mProgressSlider.setOnMouseMoved(new EventHandler<Event>() {
					@Override
					public void handle(Event event) {
						isModed = true;
					}
				});

				/**
				 * 滑动slider、手指松开以后触发
				 */
				mProgressSlider.setOnMouseReleased(new EventHandler<Event>() {
					@Override
					public void handle(Event event) {
						isModed = false;

						double newProgress = mProgressSlider.getValue() / 100;

						long totalTimestamp = VideoDurationUpdate.getInstance().getTotalDuration();
						long newTimestamp = (long) (totalTimestamp * newProgress);
						System.out.println("newTimestamp == > " + newProgress + " " + newTimestamp);
						// 先暂停四个线程
						new Thread(new Task<Void>() {
							@Override
							protected Void call() throws Exception {
								// 1、停止继续抓取 音视频
								FrameVideoGrabberThread.getInstance().setRunning(false);
								FrameAudioGrabberThread.getInstance().setRunning(false);
								// 2、清空已经抓取的音视频
								FrameFIFO.getInstance().clearAll();
								// 3、重新设置时间
								FrameVideoGrabberThread.getInstance().setTimestamp(newTimestamp * 1000000);
								FrameAudioGrabberThread.getInstance().setTimestamp(newTimestamp * 1000000);
								// 4、在新的时间节点重新抓取
								FrameVideoGrabberThread.getInstance().setRunning(true);
								FrameAudioGrabberThread.getInstance().setRunning(true);
								return null;
							}
						}).start();
					}
				});
			}
		});
	}

	/**
	 * 更新slide的进度
	 * 
	 * @param playedDuration 当前已经播放的视频时长 (单位 秒)
	 * @param totalDuration  视频的总时长 (单位 秒)
	 */
	public void updateSlider(long playedDuration, long totalDuration) {
		if (this.isModed == false) {
			float process = playedDuration * 1.0f / totalDuration * 1.0f;
			// 注意 mProgressSlider 最大值为100, 最小值为1
			// 所以setValue参数需要通过百分比 * 100
			mProgressSlider.setValue(process * 100);
		}
	}

	/**
	 * 设置样式
	 * 
	 * @param progress
	 */
	private void setSliderStyle(float progress) {
//		progress = 0.5406215f;
		String style = String.format("-fx-background-color: linear-gradient(to right, #2D819D %.2f, #969696 %.2f);", progress, progress);
		mStackPane.setStyle(style);
	}
}
